﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Temp_20190707_AForm
{
    public partial class DockPopForm : Form
    {
        public DockPopForm()
        {
            InitializeComponent();
            base.DoubleBuffered = true;
            base.ShowInTaskbar = false;
        }

        public static DockPopForm ShowPopGroup(DockTabGroup group)
        {
            if (group == null) return null;
            var dockPanel = group.DockPanel;
            var rect = dockPanel.RectangleToScreen(new Rectangle(Point.Empty, dockPanel.Size));

            DockPopForm form = new DockPopForm();
            form.TabGroup = group;
            form.DockPanel = group.DockPanel;
            form.TagForm = group.DockPanel.FindForm();
            form.Alignment = group.Alignment;
            form.Size = new Size(0, 0);
            form.splitControl.Orientation = (form.Alignment == DockAlignment.Top || form.Alignment == DockAlignment.Bottom) ? Orientation.Horizontal : Orientation.Vertical;


            form.Show(form.TagForm);
            form.Location = rect.Location;
            form.Size = rect.Size;
            return form;
        }

        private void ResetRect(object sender, EventArgs e)
        {
            this.Visible = TagForm != null && !TagForm.Disposing && !TagForm.IsDisposed && TagForm.Visible;
            var rect = DockPanel.RectangleToScreen(new Rectangle(Point.Empty, DockPanel.Size));

            splitControl.Location = new Point(0, 0);
            splitControl.Size = rect.Size;

            //if (this.IsSplitMoving)
            //{
            //    this.Location = rect.Location;
            //    this.Size = rect.Size;
            //}
            //else
            {
                var align = this.Alignment;
                if (align == DockAlignment.Top)
                {
                    this.Location = rect.Location;
                    this.Size = new Size(rect.Width, rect.Height - splitControl.Panel2.Height);
                }
                else if (align == DockAlignment.Bottom)
                {
                    splitControl.Location = new Point(0, 0 - splitControl.Panel1.Height);
                    this.Location = new Point(rect.Location.X, rect.Location.Y + splitControl.Panel1.Height);
                    this.Size = new Size(rect.Width, rect.Height - splitControl.Panel1.Height);
                }
                else if (align == DockAlignment.Left)
                {
                    this.Location = rect.Location;
                    this.Size = new Size(rect.Width - splitControl.Panel2.Width, rect.Height);
                }
                else if (align == DockAlignment.Right)
                {
                    splitControl.Location = new Point(0 - splitControl.Panel1.Width, 0);
                    this.Location = new Point(rect.Location.X + splitControl.Panel1.Width, rect.Location.Y);
                    this.Size = new Size(rect.Width - splitControl.Panel1.Width, rect.Height);
                }
            }
        }


        public Form TagForm { get; private set; }
        public DockPanel DockPanel { get; private set; }
        public DockTabGroup TabGroup { get; private set; }
        public DockAlignment Alignment { get; private set; }
        public bool IsSplitMoving { get; private set; }

        private void Form2_Load(object sender, EventArgs e)
        {
            if (TagForm != null)
            {
                TagForm.LocationChanged += ResetRect;
                TagForm.VisibleChanged += ResetRect;
            }
            if (DockPanel != null)
            {
                DockPanel.LocationChanged += ResetRect;
                DockPanel.SizeChanged += ResetRect;
            }

            ResetRect(this, EventArgs.Empty);
        }
        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            Try(() => { if (TagForm != null) TagForm.LocationChanged -= ResetRect; });
            if (DockPanel != null)
            {
                Try(() => { DockPanel.LocationChanged -= ResetRect; });
                Try(() => { DockPanel.SizeChanged -= ResetRect; });
            }

            DockPanel = null;
            TagForm = null;
        }
        private void splitControl_SplitterMoving(object sender, SplitterCancelEventArgs e)
        {
            IsSplitMoving = true;
            ResetRect(this, EventArgs.Empty);
        }
        private void splitControl_SplitterMoved(object sender, SplitterEventArgs e)
        {
            IsSplitMoving = false;
            ResetRect(this, EventArgs.Empty);
        }




        public static void Try(Action action)
        {
            try { action(); }
            catch (ThreadAbortException) { throw; }
            catch (Exception ex) { Console.WriteLine(ex); }
        }





    }
}
